<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/11/30
 * Time: 11:00
 */

namespace app\sports_admin\controller;
use app\sports_admin\model\CustomerType;
use app\sports_admin\model\Customer as CustomerModel;
use app\sports_admin\service\Customer as CustomerService;
use think\Exception;
use think\Request;
use app\exception\CustomerException;
class Customer extends Base
{
    protected $beforeActionList = [
        'isResource'=>['only'=>'showlistpage,add,useradd,customermessage,updatecustomer,endcustomer'],
        'operationSelect'=>['only'=>'showlistpage,showlistall,add,customermessage'],
        'operationInsert'=>['only'=>'useradd'],
        'operationUpdate'=>['only'=>'updatecustomer'],
        'operationDelete'=>['only'=>'endcustomer']
    ];
    //展示全部的代理的页面
    public function showListPage()
    {
        $type = CustomerType::select();
        return view('list',compact('type'));
    }
    //分页展示全部代理人信息
    public function showListAll()
    {
        //$message = CustomerService::showList();
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','type']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $result = CustomerModel::showListAll($page,$rows,$sort,$sortOrder,$searchName);
        return json($result);
    }
    //添加代理页面
    public function add()
    {
        $type = CustomerType::select();
        return view('add',compact('type'));
    }
    //添加代理
    public function userAdd(Request $request)
    {
        if($request->isAjax()){
            $result = CustomerService::customerAdd();
            if($result){
                return json(['code'=>201,'msg'=>'添加成功']);
            }else{
                throw new CustomerException([
                    'msg'=>'添加失败',
                    'errorCode'=>'400'
                ]);
            }
        }
    }
    //修改代理信息页面
    public function customerMessage()
    {
        $message = Request::instance()->only('id');
        $id      = $message['id'];
        $result = CustomerModel::selectCustomer($id);
        $type   = CustomerType::select();
        if(!$result){
            throw new CustomerException([
                'msg'=>'修改失败',
                'errorCode'=>'400'
            ]);
        }else{
            return view('update',compact('result','type'));
        }
    }
    //修改代理信息
    public function updateCustomer()
    {
        $message = Request::instance()->only('id,name,phone,tel,remark,email,type');
        $id      = $message['id'];
        $name    = $message['name'];
        $phone   = $message['phone'];
        $tel     = $message['tel'];
        $remark  = $message['remark'];
        $email   = $message['email'];
        $type    = $message['type'];
        $result  = CustomerModel::updateCustomer($id,$name,$phone,$tel,$remark,$email,$type);
        if(!$result){
            throw new CustomerException([
                'msg'=>'修改失败',
                'errorCode'=>'400'
            ]);
        }else{
            return json(['code'=>201,'msg'=>'修改成功']);
        }
    }
    //删除代理信息
    public function endCustomer()
    {
        $message = Request::instance()->only("id");
        $id      = $message['id'];
        $result  = CustomerModel::endCustomer($id);
        if($result){
            return json(['code'=>201,'msg'=>'删除成功']);
        }else{
            throw new CustomerException([
                'msg'=>'修改失败',
                'errorCode'=>'400'
            ]);
        }
    }

}